package com.google.code.sharecontent.action;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.web.RequestParameter;

import com.google.code.sharecontent.model.Content;
import com.google.code.sharecontent.model.ContentDisplay;
import com.google.code.sharecontent.model.Playlist;
import com.google.code.sharecontent.model.PlaylistContent;
import com.google.code.sharecontent.model.Status;
import com.google.code.sharecontent.model.User;

@Scope(ScopeType.CONVERSATION)
@Name("editplaylist")
public class EdiPlaylistAction extends BaseAction {

	String name;
	String description;
	String sort;
	Boolean publicPlayList;
	String contentSearch;
	List<ContentDisplay> searchedContents;
	List<ContentDisplay> extendedContents;
	String extendedContentIds;

	@In("daoAction")
	private DaoAction daoAction;

	@In("scanApplication")
	private ScanApplication scanApplication;

	@In
	User user;

	@RequestParameter("playlistId")
	String playlistId;
	Playlist selectedPlaylist;

	@Begin(nested = true)
	public void start() {
		log.info("start nested");
		/**
		 * FIXME Check access!!!
		 */
		if (this.playlistId != null && this.playlistId.length() > 0) {
			try {
				this.selectedPlaylist = daoAction.getPlaylistDao().get(new Integer(this.playlistId));
			} catch (NumberFormatException nfe) {
				log.info("error openned playlist");
			} catch (SQLException e) {
				log.error(e.getMessage());
			}
		}
		if (this.selectedPlaylist == null) {
			this.selectedPlaylist = new Playlist();
		}
		eraseValues();
		initData();
	}

	private void eraseValues() {
		searchedContents = null;
		extendedContents = null;
		extendedContentIds = null;
	}

	public void addContentToPLaylist() {
		if (this.searchedContents != null) {
			for (ContentDisplay content : this.searchedContents) {
				if (!content.getSelect().equals(content.getSelectPrevValue())) {
					log.info("add content to display " + content.getTitle());
					PlaylistContent playlistContent = new PlaylistContent();
					playlistContent.setContentId(content.getId());
					playlistContent.setDateCreated(new Date());
					playlistContent.setOrder(new Integer(0));
					playlistContent.setPlaylistId(this.selectedPlaylist.getId());
					playlistContent.setUserId(this.user.getId());
					log.info("playlistContent " + playlistContent);
					try {
						this.daoAction.getPlaylistContentDao().save(playlistContent);
					} catch (SQLException e) {
						e.printStackTrace();
						/**
						 * FIXME error navigation!
						 */
					}
				}
			}
		}
		eraseValues();
	}

	public void changeContentSearch() {
		this.searchedContents = null;
	}

	public List<ContentDisplay> getSearchedContents() {
		if (this.searchedContents == null) {
			List<Content> list = new ArrayList<Content>();
			List<Content> sublist = null;
			String extendedContentIds = getExtendedContentIds();
			if (contentSearch == null || contentSearch.length() == 0) {
				// sublist =
				// this.daoAction.getContentDao().listByStatus(scanApplication.getStatusActive().getId());
			} else {
				try {
					sublist = this.daoAction.getContentDao().findByPhraseAndStatus(contentSearch, scanApplication.getStatusActive().getId(),
							extendedContentIds);
				} catch (SQLException e) {
					log.error(e.getMessage());
				}
			}
			if (sublist != null) {
				list.addAll(sublist);
			}
			this.searchedContents = new ArrayList<ContentDisplay>();
			for (Content resource : list) {
				this.searchedContents.add(createContentDisplay(resource));
			}
			log.info("getSearchedContents " + list.size());
		}
		return searchedContents;
	}

	public List<ContentDisplay> getExtendedContents() {
		if (this.extendedContents == null) {
			String extendedContentIds = getExtendedContentIds();
			this.extendedContents = new ArrayList<ContentDisplay>();
			try {
				List<Content> sublist = this.daoAction.getContentDao().list(" ID in (" + extendedContentIds + ")", null);
				for (Content content : sublist) {
					this.extendedContents.add(createContentDisplay(content));
				}
			} catch (SQLException e) {
				log.error(e.getMessage());
			}
		}
		return this.extendedContents;
	}

	private String getExtendedContentIds() {
		if (this.extendedContentIds == null) {
			this.extendedContentIds = "-1";
			List<PlaylistContent> playlistContents;
			try {
				playlistContents = this.daoAction.getPlaylistContentDao().findByPlaylistId(this.selectedPlaylist.getId());
				for (PlaylistContent playlistContent : playlistContents) {
					extendedContentIds += ", ";
					extendedContentIds += playlistContent.getContentId().toString();
				}
			} catch (SQLException e) {
				log.error(e.getMessage());
			}
		}
		return extendedContentIds;
	}

	public void setSearchedContents(List<ContentDisplay> contentDisplays) {
		this.searchedContents = contentDisplays;
	}

	protected ContentDisplay createContentDisplay(Content content) {
		ContentDisplay resourceDisplay = ContentDisplay.createContentDisplay(content);
		try {
			Status status = this.daoAction.getStatusDao().get(content.getStatusId());
			resourceDisplay.setStatus(status.getName());
		} catch (SQLException e) {
			log.error(e.getMessage());
		}
		return resourceDisplay;
	}

	public String cancel() {
		return "ok";
	}

	public String cancelContent() {
		return "ok";
	}

	public String editContent() {
		return "ok";
	}

	public String save() {
		if (this.name != null && this.name.length() > 0) {
			selectedPlaylist.setName(this.name);
			selectedPlaylist.setDescription(this.description);
			selectedPlaylist.setSortType(0);// FIXME исправить сортировку
			selectedPlaylist.setPublicPlaylist(this.publicPlayList);
			selectedPlaylist.setUserId(user.getId());
			selectedPlaylist.setStatusId(scanApplication.getStatusActive().getId());
			try {
				selectedPlaylist = daoAction.getPlaylistDao().save(selectedPlaylist);
				if (selectedPlaylist != null && selectedPlaylist.getId() != null) {
					return "ok";
				}
			} catch (SQLException e) {
				log.error(e.getMessage());
			}
		}
		return "error";
	}

	private void initData() {
		if (this.selectedPlaylist != null) {
			this.setName(this.selectedPlaylist.getName());
			this.setDescription(this.selectedPlaylist.getDescription());
			this.setPublicPlayList(this.getPublicPlayList());
			this.setSort(this.getSort());
		}
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public Boolean getPublicPlayList() {
		return publicPlayList;
	}

	public void setPublicPlayList(Boolean publicPlayList) {
		this.publicPlayList = publicPlayList;
	}

	public String getContentSearch() {
		return contentSearch;
	}

	public void setContentSearch(String contentSearch) {
		this.contentSearch = contentSearch;
	}

}
